
window.onload=function(){
	//示例一
	var iTgl=new k_iToggle();
		iTgl.init({
			oid:{boxId:'box',numId:'num',imgId:'imgs'},
			isAuto:true,
			aCls:'act',
			iCls:'imgs',
			direct:'lvl'
		});
		chg(iTgl,'chg');
		
	//改变方向
	function chg(o,id){
		$('#'+id).click(function(){
			if(o.direct=='crt'){
				o.direct='lvl';
				$('#'+o.ids[2]).addClass(o.iCls);
				$(this).text('纵向');
			}else{
				o.direct='crt';
				$(this).text('横向');
				$('#'+o.ids[2]).removeClass(o.iCls);
			}
		});
	}
}
//***************************************************************************
//初始化参数说明0--------------------------------
//init({n:0,oid:{boxId:'',numId:'',imgId:''},aCls:'',iCls:'',direct:'',isAuto:false});
//***oid：{}对象，有三个属性：boxId:容器ID,numId:序号的ID，imgId:图片ID，必选
//***aCls:序号中高亮样式，必选
//***iCls:让图片水平排列样式，必选
//***初始化参数：{direct:'',isAuto:false,oid:{boxId:'',numId:'',imgId:''},aCls:''}
//***direct:方向，'lvl',水平，crt:垂直，可选
//***isAuto:自动播放，true/false，可选
//***n:默认当前为第一个，可选
//
//****************************************************************************/

var k_iToggle=function(){
	var _this=this;
		this.n=0;
		this.direct='crt';
		this.isAuto=false;
		this.init=function(o){
				if(o.oid.boxId&&o.oid.numId&&o.oid.imgId){
					_this.ids=[o.oid.boxId,o.oid.numId,o.oid.imgId];
				}else{
					alert('缺少ID');return;
				}
				_this.isAuto=!!o.isAuto;
				_this.aCls=''+o.aCls;
				_this.iCls=''+o.iCls
				o.n&&(_this.n=o.n);
				_this.direct=o.direct!='lvl'?'crt':'lvl';
				if(_this.direct=='lvl'){
					$('#'+_this.ids[2]).addClass(_this.iCls);
				}else{
					$('#'+_this.ids[2]).removeClass(_this.iCls);
				}
				_this.evtNum();
				if(_this.isAuto){_this.auto();}
			}
		this.evtNum=function(){
				var ls=$('#'+_this.ids[1]+' li');
				_this.numLen=ls.length;
				ls.each(function(i,o){
					var t=_this.isAuto;
					$(this).hover(function(){
							$(ls[_this.n]).removeClass(_this.aCls);
							$(this).addClass(_this.aCls);
							_this.n=i;
							_this.isAuto=false;
							clearTimeout(_this._auto);
							_this.run();						
					},
					function(){
						_this.isAuto=t;
						_this.auto();
					});	
				});
			}
		this.Tween=function(t,b,c,d){
				return c*((t=t/d-1)*t*t + 1) + b;
			}
		this.auto=function(){
				if(_this.isAuto){
					clearTimeout(_this._auto);
					_this._auto=setTimeout(function(){
						var o=$('#'+_this.ids[1]+' li');
						o.eq(_this.n).removeClass(_this.aCls);
						if(_this.n<_this.numLen-1){_this.n++;}else{_this.n=0;}
						o.eq(_this.n).addClass(_this.aCls);
						_this.run();
					},2000);
				}
			}
		this.run=function(){
			var _demo=$('#'+_this.ids[0])[0],
				sn=_this.direct=='crt'?_demo.scrollTop:_demo.scrollLeft,
				sl=_this.direct=='crt'?_demo.scrollHeight:_demo.scrollWidth,
				sc=parseInt(sl*1*_this.n/_this.numLen)-sn,
				t=0,b=sn,c=sc,d=60;
				clearTimeout(_this._t);
				(function _move(){
					if(t<d){
						t++;
						if(_this.direct=='crt'){
							_demo.scrollTop=_this.Tween(t,b,c,d);
						}else{
							_demo.scrollLeft=_this.Tween(t,b,c,d);
						}
						_this._t=setTimeout(_move,5);
					}else{
						_this.auto();
					}
				})();
			}
}